package com.example.coder_voucher.controller;

import com.example.coder_voucher.dto.ApiResponse;
import com.example.coder_voucher.dto.ClaimVoucherRequest;
import com.example.coder_voucher.entity.UserVoucher;
import com.example.coder_voucher.entity.Voucher;
import com.example.coder_voucher.service.VoucherService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/vouchers")
@CrossOrigin(origins = "*")
public class VoucherController {
    
    @Autowired
    private VoucherService voucherService;
    
    /**
     * Get all available vouchers
     */
    @GetMapping("/available")
    public ResponseEntity<ApiResponse<List<Voucher>>> getAvailableVouchers() {
        List<Voucher> vouchers = voucherService.getAvailableVouchers();
        return ResponseEntity.ok(ApiResponse.success("Available vouchers retrieved successfully", vouchers));
    }
    
    /**
     * Get all vouchers
     */
    @GetMapping
    public ResponseEntity<ApiResponse<List<Voucher>>> getAllVouchers() {
        List<Voucher> vouchers = voucherService.getAllVouchers();
        return ResponseEntity.ok(ApiResponse.success("All vouchers retrieved successfully", vouchers));
    }
    
    /**
     * Get voucher by code
     */
    @GetMapping("/{code}")
    public ResponseEntity<ApiResponse<Voucher>> getVoucherByCode(@PathVariable String code) {
        Voucher voucher = voucherService.getVoucherByCode(code);
        return ResponseEntity.ok(ApiResponse.success("Voucher retrieved successfully", voucher));
    }
    
    /**
     * Claim a voucher
     */
    @PostMapping("/claim")
    public ResponseEntity<ApiResponse<UserVoucher>> claimVoucher(@Valid @RequestBody ClaimVoucherRequest request) {
        UserVoucher userVoucher = voucherService.claimVoucher(request.getUsername(), request.getVoucherCode());
        return ResponseEntity.status(HttpStatus.CREATED)
            .body(ApiResponse.success("Voucher claimed successfully", userVoucher));
    }
    
    /**
     * Rollback a voucher
     */
    @PostMapping("/rollback")
    public ResponseEntity<ApiResponse<UserVoucher>> rollbackVoucher(@Valid @RequestBody ClaimVoucherRequest request) {
        UserVoucher userVoucher = voucherService.rollbackVoucher(request.getUsername(), request.getVoucherCode());
        return ResponseEntity.ok(ApiResponse.success("Voucher rolled back successfully", userVoucher));
    }
    
    /**
     * Get user's claimed vouchers
     */
    @GetMapping("/user/{username}")
    public ResponseEntity<ApiResponse<List<UserVoucher>>> getUserVouchers(@PathVariable String username) {
        List<UserVoucher> userVouchers = voucherService.getUserVouchers(username);
        return ResponseEntity.ok(ApiResponse.success("User vouchers retrieved successfully", userVouchers));
    }
    
    /**
     * Get user's rollbackable vouchers
     */
    @GetMapping("/user/{username}/rollbackable")
    public ResponseEntity<ApiResponse<List<UserVoucher>>> getRollbackableVouchers(@PathVariable String username) {
        List<UserVoucher> userVouchers = voucherService.getRollbackableVouchers(username);
        return ResponseEntity.ok(ApiResponse.success("Rollbackable vouchers retrieved successfully", userVouchers));
    }
    
    /**
     * Create a new voucher
     */
    @PostMapping
    public ResponseEntity<ApiResponse<Voucher>> createVoucher(@Valid @RequestBody Voucher voucher) {
        Voucher createdVoucher = voucherService.createVoucher(voucher);
        return ResponseEntity.status(HttpStatus.CREATED)
            .body(ApiResponse.success("Voucher created successfully", createdVoucher));
    }
    
    /**
     * Mark voucher as used
     */
    @PostMapping("/use")
    public ResponseEntity<ApiResponse<String>> useVoucher(@Valid @RequestBody ClaimVoucherRequest request) {
        voucherService.markVoucherAsUsed(request.getUsername(), request.getVoucherCode());
        return ResponseEntity.ok(ApiResponse.success("Voucher marked as used successfully", "OK"));
    }
    
    /**
     * Get current inventory from Redis
     */
    @GetMapping("/inventory/{voucherId}")
    public ResponseEntity<ApiResponse<Long>> getCurrentInventory(@PathVariable Long voucherId) {
        Long inventory = voucherService.getCurrentInventory(voucherId);
        return ResponseEntity.ok(ApiResponse.success("Current inventory retrieved successfully", inventory));
    }
}